﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class ThuocDAO
    {
        public static DataTable LayDanhSachThuoc()
        {
            string sql;
            sql = "SELECT * FROM [Thuoc]";
            return DataAccess.ExecQuery(sql);
        }

        public static void ThemThuoc(ThuocDTO obj)
        {
            string sql = "";
            sql = string.Format(@"INSERT INTO [Thuoc](TenThuoc, GiaTien, SoLuongTon, DonViTinh) VALUES(N'{0}', N'{1}', N'{2}', N'{3}')",
                                                obj.TenThuoc, obj.GiaTien, obj.SoLuongTon, obj.DonViTinh);
            DataAccess.ExecNonQuery(sql);
        }

        public static void SuaThuoc(ThuocDTO obj)
        {
            string sql = "";
            sql = string.Format(@"UPDATE [Thuoc]
                                SET TenThuoc = N'{0}', GiaTien = N'{1}', SoLuongTon = N'{2}', DonViTinh = N'{3}'
                                WHERE MaThuoc = {4}", obj.TenThuoc, obj.GiaTien, obj.SoLuongTon, obj.DonViTinh);
            DataAccess.ExecNonQuery(sql);
        }

        public static void XoaThuoc(int maThuoc)
        {
            string sql;
            sql = string.Format("DELETE [Thuoc] WHERE MaThuoc = {0}", maThuoc);
            DataAccess.ExecNonQuery(sql);
        }

        public static ThuocDTO LayThongTinThuocTheoMaThuoc(int maThuoc)
        {
            ThuocDTO kq = new ThuocDTO();
            string sql;
            sql = @"select * FROM [Thuoc]
                    where MaThuoc = " + maThuoc;
            DataTable dt = DataAccess.ExecQuery(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                kq.MaThuoc = Convert.ToInt32(dt.Rows[0]["MaThuoc"]);
                kq.TenThuoc = Convert.ToString(dt.Rows[0]["TenThuoc"]);
                kq.GiaTien = Convert.ToDouble(dt.Rows[0]["GiaTien"]);
                kq.SoLuongTon = Convert.ToInt32(dt.Rows[0]["SoLuongTon"]);
                kq.DonViTinh = Convert.ToString(dt.Rows[0]["DonViTinh"]);                
            }
            return kq;
        }
    }
}
